import {
  userCertList,
  userCertUpdateStatus,
  userWithDrawList,
  userWithDrawUpdate,
  userWithDrawStatus,
} from '@/services/appuser';
import { message } from 'antd';

export default {
  namespace: 'appuser',

  state: {
    list: [],
    pagination: [],
  },

  effects: {
    *fetchUserCertList({ payload }, { call, put }) {
      const response = yield call(userCertList, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'userCertList',
          payload: response.data,
        });
      }
    },
    *fetchUserCertUpdateStatus({ payload }, { call, put }) {
      const response = yield call(userCertUpdateStatus, payload);
      if (response && response.status === 'success') {
        yield put({
          type: 'userCertUpdateStatus',
          ...payload,
        });
      }
    },
    *fetchUserWithDrawList({ payload }, { call, put }) {
      const response = yield call(userWithDrawList, payload);

      if (response && response.status === 'success') {
        yield put({
          type: 'userWithDrawList',
          payload: response.data,
        });
      }
    },
    *fetchUserWithDrawStatus({ payload }, { call, put }) {
      const response = yield call(userWithDrawStatus, payload);
      if (response && response.status === 'success') {
        message.success('审核成功');
        yield put({
          type: 'userWithDrawStatus',
          payload: payload,
        });
      }
    },
  },

  reducers: {
    userCertList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    userCertUpdateStatus(state, action) {
      for (let i = 0; i < state.list.list.length; i++) {
        if (state.list.list[i].id === action.id) {
          state.list.list[i].status = action.status;
        }
      }
      return {
        ...state,
      };
    },
    userWithDrawList(state, action) {
      return {
        ...state,
        list: action.payload,
        pagination: action.payload.pagination,
      };
    },
    userWithDrawStatus(state, action) {
      for (let i = 0; i < state.list.list.length; i++) {
        if (state.list.list[i].id === action.payload.id) {
          state.list.list[i].status = action.payload.status;
        }
      }
      return {
        ...state,
      };
    },
  },
};
